package birdry.ui.components
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;

	public class InteractiveUIComponent extends UIComponent
	{		
					
		public function InteractiveUIComponent(view : DisplayObject) 
		{
			super(view);				
		}
	

		protected function onClick(event : MouseEvent) : void
		{
			dispatchEvent(event);
		}

		protected function onMouseDown(event : MouseEvent) : void
		{
			dispatchEvent(event);
		}

		protected function onMouseUp(event : MouseEvent) : void
		{
			dispatchEvent(event);
		}

		protected function onMouseOver(event : MouseEvent) : void
		{
			dispatchEvent(event);			
		}

		protected function onMouseOut(event : MouseEvent) : void
		{
			dispatchEvent(event);
		}

		protected function onRollOver(event : MouseEvent) : void
		{
			dispatchEvent(event);
		}

		protected function onRollOut(event : MouseEvent) : void
		{
			dispatchEvent(event);
		}

		protected function onMouseMove(event : MouseEvent) : void
		{
			dispatchEvent(event);
		}

		protected function onMouseWheel(event : MouseEvent) : void
		{
			dispatchEvent(event);
		}

		protected function setListener(target : EventDispatcher, type : String, listener : Function, isAdd : Boolean) : void
		{
			var isActive : Boolean = target.hasEventListener(type);
			if (isAdd && !isActive) target.addEventListener(type, listener);
			else if (!isAdd && isActive) target.removeEventListener(type, listener);
		}



		protected function onKeyUp(event : KeyboardEvent) : void
		{
			dispatchEvent(event);
		}

		protected function onKeyDown(event : KeyboardEvent) : void
		{
			dispatchEvent(event);
		}

// ----------------------------------------------------------------------------
// Public Method
// ----------------------------------------------------------------------------
		
		public function setMouseEventActivation(clickEvent : Boolean = true, moveEvent : Boolean = false, wheelEvent : Boolean = false) : void 
		{
			setListener(view, MouseEvent.CLICK, onClick, clickEvent);
			setListener(view, MouseEvent.MOUSE_DOWN, onMouseDown, clickEvent);
			setListener(view, MouseEvent.MOUSE_UP, onMouseUp, clickEvent);
			setListener(view, MouseEvent.MOUSE_OVER, onMouseOver, clickEvent);
			setListener(view, MouseEvent.MOUSE_OUT, onMouseOut, clickEvent);
			setListener(view, MouseEvent.ROLL_OVER, onRollOver, clickEvent);						
			setListener(view, MouseEvent.ROLL_OUT, onRollOut, clickEvent);			
			setListener(view, MouseEvent.MOUSE_MOVE, onMouseMove, moveEvent);			
			setListener(view, MouseEvent.MOUSE_WHEEL, onMouseWheel, wheelEvent);	
		}
		
		public function setKeyEventActivation(keyEvent : Boolean) : void 
		{
			setListener(view, KeyboardEvent.KEY_UP, onKeyUp,  keyEvent);
			setListener(view, KeyboardEvent.KEY_DOWN, onKeyDown, keyEvent);
		}
		
		public function setPhase(frame : Object, isPlay : Boolean = false) : void 
		{				
			if (view is MovieClip)
			{
				try
				{
					if (isPlay) MovieClip(_view).gotoAndPlay(frame);
					else MovieClip(_view).gotoAndStop(frame);
				}
				catch(err1 : Error)
				{
					throw new Error("[" + frame + "]는 존재하지 않는 프레임 입니다.");					
				}
			}	
		}

		override public function destroy() : void
		{			
			setMouseEventActivation(false);
			setKeyEventActivation(false);
			super.destroy();
		}

		public function setMouseEnable(enable : Boolean) : void
		{
			if (_view is Sprite)
			{
				_view["mouseEnabled"] = enable;
				_view["mouseChildren"] = enable;
			}			
		}			
	} // end class
} // end package